﻿@using eLoan.Models;
@model VoidPaymentModel

@{
    ViewBag.Title = "Void Loan Payment";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Void Loan Payment</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li class="active">
                <strong>Void Loan Payment</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("SaveVoidLoanPayment", "Workspace", FormMethod.Post, new { id = "saveForm" }))
            {
                <fieldset>
                    <div class="ibox-title">
                        <div class="divSearch">
                            <table class="table-search">
                                <tr>
                                    <td class="tdFixed">
                                        <span>@Html.LabelFor(m => m.Branches)</span>
                                        @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line", @onchange = "GetAgents();" })
                                    </td>
                                    <td class="tdFixed">
                                        @Html.CheckBoxFor(m => m.IsAgentSelected, new { @id = "chkAgent", @onclick = "SetAgent();" })
                                        <span>@Html.LabelFor(m => m.Agents)</span>
                                        @Html.DropDownListFor(m => m.SelectedAgentID, Model.Agents, new { @id = "ddlAgents", @class = "text-box single-line" })
                                    </td>
                                    @if (Model.IsAdmin)
                                    { 
                                    <td class="tdFixed">
                                        @Html.LabelFor(m => m.DatePosted)
                                        <div class="form-group" id="dp">
                                            <div class="input-group date">
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar"></i>
                                                </span>
                                                @Html.TextBoxFor(m => m.DatePosted, "{0:MM/dd/yyyy}", new { @id = "txtDatePosted", @class = "form-control" })
                                            </div>
                                        </div>
                                    </td>
                                    }
                                    <td class="tdFixed">
                                        <span class="span">@Html.LabelFor(m => m.SearchText)</span>
                                        @Html.TextBoxFor(m => m.SearchText, new { @id = "txtSearchText", @class = "single-line", @width = "50%" })
                                    </td>
                                    <td class="tdFixed">
                                        @if (!Model.IsAdmin) { @Html.HiddenFor(m => Model.DatePosted) } 
                                        <button type="submit" id="btnRefresh" class="btn btn-primary btnSearch" value="Refresh">Search</button>
                                    </td>
                                    <td class="tdAlignRight" style="vertical-align:bottom; padding-bottom: 6px">
                                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#confirm-void">Void selected payments</button>
                                        <button type="submit" id="btnSave" style="display:none" name="command" value="Void">Void selected payments</button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    
                    <div class="ibox-content">
                        
                        <table id="tbl" class="table table-bordered table-List">
                            <thead class="k-grid-header">
                                <tr>
                                    <th>Borrower</th>
                                    <th>Agent</th>
                                    <th>Document No</th>
                                    <th>Payment Date</th>
                                    <th>Payment Type</th>
                                    <th>Payment</th>
                                    <th>Paid Penalty</th>
                                    <th>Remarks</th>
                                    <th>Status</th>
                                    <th>
                                        @Html.CheckBox("chkCheckAll", false, new { @class = "checkAll", @onchange = "checkAll(this)" })
                                    </th>
                                </tr>
                            </thead>
                            <tbody id="tableBody">
                                @for (var i = 0; i < Model.VoidLoanPaymentItems.Count(); i++)
                                {
                                    <tr>
                                        <td>
                                            @Html.ValueFor(m => m.VoidLoanPaymentItems[i].BorrowerName)
                                            @Html.HiddenFor(m => m.VoidLoanPaymentItems[i].PaymentID)
                                        </td>
                                        <td>@Html.ValueFor(m => m.VoidLoanPaymentItems[i].AgentName)</td>
                                        <td>@Html.ActionLink(Model.VoidLoanPaymentItems[i].DocumentNo, "LoanDetails", "Loan", null, null, null, new { documentNo = Model.VoidLoanPaymentItems[i].DocumentNo }, null)</td>
                                        <td>@Html.ValueFor(m => m.VoidLoanPaymentItems[i].PaymentDate, "{0:MMM/dd/yyyy}")</td>
                                        <td>@Html.ValueFor(m => m.VoidLoanPaymentItems[i].PaymentTypeName)</td>
                                        <td style="text-align:right">@Html.ValueFor(m => m.VoidLoanPaymentItems[i].PaymentAmount, "{0:#,###,##0.00}")</td>
                                        <td style="text-align:right">@Html.ValueFor(m => m.VoidLoanPaymentItems[i].PaidPenalty, "{0:#,###,##0.00}")</td>
                                        <td>@Html.TextBoxFor(m => m.VoidLoanPaymentItems[i].Remarks, new { @class = "single-line scroll-field1", @style = "width:150px;" })</td>
                                        <td>@Html.ValueFor(m => m.VoidLoanPaymentItems[i].PaymentStatus)</td>
                                        <td>@Html.CheckBoxFor(m => m.VoidLoanPaymentItems[i].IsSelected, new { @class = "check", @onchange = "checkMe()" })</td>
                                    </tr>
                                }
                            </tbody>

                        </table>
                        

                        <div class="modal fade" id="confirm-void" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">

                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
                                    </div>

                                    <div class="modal-body">
                                        <p>Are you sure you want to void the selected payments?</p>
                                        <p class="debug-url"></p>
                                    </div>

                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
                                        <button type="button" class="btn btn-primary btn-ok" data-dismiss="modal">Yes</button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <button type="submit" id="btnRefresh" style="display:none" name="command" value="Refresh">Refresh</button>
                    </div>
                    
            </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
    @Styles.Render("~/plugins/dataPickerStyles")

    <style>
    select[disabled], select[disabled]:hover {
        pointer-events: none;
        color: #e1e1e1;
    }

    .divScroll {
        height: 400px;
        overflow-x: scroll;
        border: solid 1px #EBEBEB;
    }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
        margin-top: 5px;
    }

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search td {
            margin: 10px 10px 10px 10px;
            padding: 10px 10px 10px 10px;
            vertical-align: bottom;
        }

        .table-search .tdAlignRight {
            text-align: right;
        }

        .table-search .tdFixed {
            width: 20%;
        }

            .table-search .tdFixed div {
                margin-bottom: 0px;
            }

        .table-search td .btnSearch {
            margin-bottom: 0px;
        }


        .table-search td span {
            margin-left: 7px;
        }
</style>
}

@section Scripts{
@Scripts.Render("~/plugins/dataPicker")
@Scripts.Render("~/plugins/dataTables")

    <script type="text/javascript">

    $(document).ready(function () {

        $('.table-List').dataTable({
            ordering: true,
            searching: false,
            paging: false,
            scrollY: 400
        });
        
        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                Refresh();
                return false;
            }
        });

        $('#confirm-void').on('click', '.btn-ok', function (e) {
            document.getElementById('btnSave').click();
        });

        SetDateFormat();
        SetAgent();

    });

    function Refresh() {
        document.getElementById('btnRefresh').click();
    }

    function checkAll(me) {
        var val = $(me).is(':checked');

        $('.check').each(function () {
            $(this).prop('checked', val);
        });
    }

    function checkMe() {
        var allIsChecked = true;

        $('.check').each(function () {
            val = $(this).is(':checked');

            if (!val)
                allIsChecked = false;
        });

        $('#chkCheckAll').prop('checked', allIsChecked);

    }

    function SetDateFormat() {

        $('#dp .input-group.date').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            format: 'mm/dd/yyyy',
            autoclose: true

        });

        var dp = $('#txtDatePosted').val();
        $('#dp .input-group.date').datepicker("update", new Date(dp));

    }

    function SetAgent() {
        var isChecked = $('#chkAgent').is(":checked");

        if (isChecked) {
            $('#ddlAgents').removeAttr('disabled');
        }
        else {
            $('#ddlAgents').attr('disabled', 'disabled');
        }
    }

    function GetAgents()
    {
        var val = $('#ddlBranches').val();
        var subItems = "";

        $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
    function (data) {

        $.each(data, function (index, item) {

            subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"

        });

        $("#ddlAgents").html(subItems)
    });
    }

    function Refresh() {
        document.getElementById('btnRefresh').click();
    }

</script>



}